#! /bin/sh

cleanup () {
  test -z "$erl" || kill $erl
}

erl -pa ../src -sname nodefinder_test1 -eval '
  nodefindersup:start_link ({ 226, 0, 0, 1 }, 6969),
  receive after 10000 -> ok end
' -noshell -noinput -s init stop &

erl=$!

trap cleanup INT QUIT TERM

sleep 1

erl -pa ../src -sname nodefinder_test2 -eval '
  "nodefinder_test2@" ++ Host = atom_to_list (node ()),
  [] = erlang:nodes (),
  nodefindersup:start_link ({ 226, 0, 0, 1 }, 6969),
  receive after 1000 -> ok end,
  Other = list_to_atom ("nodefinder_test1@" ++ Host),
  [ Other ] = erlang:nodes (),
  rpc:call (Other, init, stop, [])
' -noshell -noinput -s init stop || { cleanup; exit 1; }

wait

exit 0
